package trss.webapp.view;

import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;

import trss.model.Torrent;

@Component("torrentDownloadView")
public class TorrentDownloadView extends AbstractView {
    private static final Logger log = LoggerFactory.getLogger(TorrentDownloadView.class);
    private static final String CONTENT_TYPE = "application/x-bittorrent";

    /**
    * ${@inheritDoc}
    */
    @Override
    public String getContentType() {
        return CONTENT_TYPE;
    }

    /**
     * ${@inheritDoc}
     */
     @Override
     protected void renderMergedOutputModel(Map<String, Object> model,
             HttpServletRequest req, HttpServletResponse resp) throws Exception {
         resp.setContentType(CONTENT_TYPE);
         Torrent torrent = (Torrent) model.get("torrent");
         resp.setContentLength(torrent.getFileSize());
         resp.setBufferSize(torrent.getFileSize());
         ServletOutputStream os = resp.getOutputStream();
         byte[] buf = torrent.getOrigData();
         os.write(buf);
         os.flush();
     }
     
}
